package com.androidbook;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;

public class Monster implements MechanicGame, GameObject
{
  public Direct monsterFacing;
   public int posX;
   public int posY;
   private boolean inv;
   private int zegar;
   private int licznikAkcji;
   private boolean atak;
   int przesX;
   int przesY;
   public Monster()
   {
	   atak=false;
	   zegar=0;
	   licznikAkcji=0;
	   posX=6;
	   posY=4;
	   monsterFacing = Direct.EAST;
   }
   /* (non-Javadoc)
 * @see com.androidbook.GameObject#move(int[][], int, int)
 */
public int move(int mapa[][],int p_x, int p_y)
   {
	   int ruch=0;
	   this.atak=false;
	   if( (p_x-1==posX)  && (p_y==posY))
		   this.atak=true;  
	   if(  (p_x+1==posX) && (p_y==posY))
		   this.atak=true;  
	     if((p_y-1==posY) && (p_x==posX))
		   this.atak=true;
	     if((p_y+1==posY) && (p_x==posX))
			   this.atak=true; 
	     if((p_y==posY) && (p_x==posX))
			   this.atak=true; 
	   if (!atak)
	   {
	   licznikAkcji++;
	   if (licznikAkcji>30)
	   {
		   licznikAkcji=0;
	   Random rand = new Random();
		ruch = rand.nextInt(4) + 1;
		switch(ruch)
		{
			case 1:
				if(mapa[posX+1][posY]!=1)
				  this.posX++;
				else
					ruch=0;
				break;
			case 2:
				if(mapa[posX][posY+1]!=1)
				  this.posY++;
				else
					ruch=0;
				break;
			case 3:
				if(mapa[posX-1][posY]!=1)				
				  this.posX--;
				else
					ruch=0;
				break;
			case 4:
				if(mapa[posX][posY-1]!=1)
				  this.posY--;
				else
					ruch=0;
				break;
			default:
				break;
		}
	   }
	   }
		return ruch;
   }
   
   /* (non-Javadoc)
 * @see com.androidbook.GameObject#PaintMonster(android.graphics.Canvas, android.graphics.Bitmap[], com.androidbook.MechanicGame.Direct, float, int, int)
 */
public void draw(Canvas canvas, Bitmap[] bmpTbl, Direct north, float scale,int px, int py)
   {
	   int bmpNbr = update(north);
	   int Width=bmpTbl[bmpNbr].getWidth();
	   int Height=bmpTbl[bmpNbr].getHeight();
	   
	   Matrix mx= new Matrix();
	   if (inv)
		mx.preScale(-1,1);
	   else
		mx.preScale(1, 1);
	   
		Bitmap Flipped;
		Flipped = Bitmap.createBitmap(bmpTbl[bmpNbr] , 0, 0,Width,Height , mx, false);
	   
	   Rect destRect;
		Rect srcRect;
		srcRect = new Rect(0, 0, Width, Height);
		destRect = new Rect(przesX+px, przesY+py, przesX+px + (int)(Width*scale), przesY+py + (int)(Height*scale));
		canvas.drawBitmap(Flipped, srcRect, destRect, null);
   }

	/**
	 * 
	 * @param north
	 * @return
	 */
	public int update(Direct north) {
		boolean skok=false;
		   boolean czekaj=false;
		   Random rand = new Random();
		   if (zegar<50)
		   {
			   czekaj=true;
		   }
		   if((zegar>50) && (zegar<60))
		   {
				przesX= rand.nextInt(2) + 1;
				przesY= rand.nextInt(2) + 1;
				skok=false;
		   }
		   if ((zegar>60) && (zegar<70))
		   {
			   przesX= rand.nextInt(2) + 1;
				przesY= rand.nextInt(2) + 1;
				skok=true;
		   }
		   if ((zegar>70) && (zegar<80))
		   {
			   przesX= rand.nextInt(2) + 1;
			   przesY= rand.nextInt(2) + 1;
		   }
		   if (zegar>100)
			   zegar=0;
		   zegar++;
		   
		   int bmpNbr = changeDirection(north);
		   if (this.atak)
			   if (skok)
				 bmpNbr=6-1;
			   else
			     bmpNbr=5-1;
		   if (czekaj)
			   bmpNbr=1-1;
		return bmpNbr;
	}
	   
   /* (non-Javadoc)
 * @see com.androidbook.GameObject#MonsterFacing(com.androidbook.MechanicGame.Direct)
 */
	public int changeDirection(Direct facing)
	{
		int bmpNbr=1;
		switch(facing)
		{
		case NORTH:
			switch(monsterFacing)
			{
			case NORTH:
				bmpNbr=4-1;
				inv=false;
				break;
			case EAST:
				bmpNbr=3-1;
				inv=true;
				break;
			case SOUTH:
				bmpNbr=1-1;
				inv=false;
				break;
			case WEST:
				bmpNbr=3-1;
				inv=false;
				break;
			default:
				break;
			}
			break;
		case EAST:
			switch(monsterFacing)
			{
			case NORTH:
				bmpNbr=3-1;
				inv=false;
				break;
			case EAST:
				bmpNbr=4-1;
				inv=false;
				break;
			case SOUTH:
				bmpNbr=3-1;
				inv=true;
				break;
			case WEST:
				bmpNbr=1-1;
				inv=false;
				break;
			default:
				break;
			}
			break;
		case SOUTH:
			switch(monsterFacing)
			{
			case NORTH:
				bmpNbr=1-1;
				inv=false;
				break;
			case EAST:
				bmpNbr=3-1;
				inv=false;
				break;
			case SOUTH:
				bmpNbr=4-1;
				inv=false;
				break;
			case WEST:
				bmpNbr=3-1;
				inv=true;
				break;
			default:
				break;
			}
			break;
		case WEST:
			switch(monsterFacing)
			{
			case NORTH:
				bmpNbr=3-1;
				inv=true;
				break;
			case EAST:
				bmpNbr=1-1;
				inv=false;
				break;
			case SOUTH:
				bmpNbr=3-1;
				inv=false;
				break;
			case WEST:
				bmpNbr=4-1;
				inv=false;
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return bmpNbr;
	}
   
}
